package com.qijian.tool.utils;

import cn.hutool.core.collection.CollUtil;
import com.qijian.common.core.domain.entity.SysRole;
import com.qijian.common.utils.SecurityUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class OrgUtil {

    /**
     * 筛选机构
     * 根据关键字 获取角色中配置的机构
     *
     * @param key
     * @return
     */
    public static List<String> getOrgNames(String key) {
        key = key + "_ORG_";
        List<String> orgNames = new ArrayList<>();
        List<SysRole> roles = SecurityUtils.getRoles();
        for (SysRole role : roles) {
            String roleKey = role.getRoleKey();
            if (!roleKey.contains(key)) {
                continue;
            }
            String[] split = roleKey.split(",");
            for (String s : split) {
                if (s.contains(key)) {
                    String orgName = s.substring(key.length());
                    orgNames.add(orgName);
                }
            }
        }

        // 去重
        if (CollUtil.isNotEmpty(orgNames)) {
            return orgNames.stream().distinct().collect(Collectors.toList());
        }
        return orgNames;
    }

    /**
     * 光伏/基建/机电竞价根据机构判断询价单是光伏类、基建类还是机电类
     *
     * @param orgName 机构
     * @return 光伏或者基建
     */
    public static String getSfbType(String orgName) {
        if (orgName.contains("电力")) {
            return orgName.contains("振江电力") ? "机电" : "光伏";
        } else {
            return "基建";
        }
    }
}
